// pages/operation/student/studentRecord/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js"
const classModel = new ClassModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentId: "", // 传过来的学生id
    activeTab: "first",
    // 已上课记录
    firstPageNum: 1,
    firstTotal: 0,
    firstTableData: [],
    firstTriggered: false,
    firstLoadStatus: "",
    // 待上课记录
    secondPageNum: 1,
    secondTotal: 0,
    secondTableData: [],
    secondTriggered: false,
    secondLoadStatus: "",
    showDetail: false,
    form: {},
    selectRow: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      studentId: options.studentId
    })
    // this.getOrdeList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      firstTriggered: false,
      firstPageNum: 1,
      firstTableData: [],
      secondTriggered: false,
      secondPageNum: 1,
      secondTableData: []
    })
    this.getOrdeList();
    this.getNotOrderList();
  },

  // tab切换
  onTabChange(e) {

    const tabName = e.detail.name;
    if (tabName == "first") {
      this.setData({
        firstTriggered: false,
        firstPageNum: 1,
        firstTableData: []
      })
      this.getOrdeList();
    } else {
      this.setData({
        secondTriggered: false,
        secondPageNum: 1,
        secondTableData: []
      })
      this.getNotOrderList();
    }
  },

  // 获取已上课记录
  getOrdeList() {
    classModel.getOrdeListApi({ pageNum: this.data.firstPageNum, pageSize: 15, listType: "1", studentId: this.data.studentId }).then(res => {
      let rows = this.data.firstTableData;
      rows.push(...res.rows);
      this.setData({
        firstTableData: rows,
        firstTotal: res.total
      })
      if (this.data.firstTotal == 0) {
        this.setData({
          firstLoadStatus: ""
        })
      }
      else if (this.data.firstTableData.length < this.data.firstTotal) {
        this.setData({
          firstLoadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          firstLoadStatus: "nomore"
        })
      }
    })
  },
  getFirstMoreData() {
    if (this.data.firstLoadStatus === "nomore") {
      return
    }
    this.setData({
      firstPageNum: this.data.firstPageNum + 1,
      firstLoadStatus: "loading"
    })
    this.getOrdeList();
  },
  refreshFirstData() {
    this.setData({
      firstTriggered: false,
      firstPageNum: 1,
      firstTableData: []
    })
    this.getOrdeList();
  },

  // 获取待上课记录
  getNotOrderList() {
    classModel.getOrdeListApi({ pageNum: this.data.secondPageNum, pageSize: 15, listType: "4", studentId: this.data.studentId }).then(res => {
      let rows = this.data.secondTableData;
      rows.push(...res.rows);
      this.setData({
        secondTableData: rows,
        secondTotal: res.total
      })
      if (this.data.secondTotal == 0) {
        this.setData({
          secondLoadStatus: ""
        })
      }
      else if (this.data.secondTableData.length < this.data.secondTotal) {
        this.setData({
          secondLoadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          secondLoadStatus: "nomore"
        })
      }
    })
  },
  getSecondMoreData() {
    if (this.data.secondLoadStatus === "nomore") {
      return
    }
    this.setData({
      secondPageNum: this.data.secondPageNum + 1,
      secondLoadStatus: "loading"
    })
    this.getNotOrderList();
  },
  refreshSecondData() {
    this.setData({
      secondTriggered: false,
      secondPageNum: 1,
      secondTableData: []
    })
    this.getNotOrderList();
  },

  openRecordDetail(e) {
    const row = e.currentTarget.dataset.query;
    this.setData({
      selectRow: row,
      showDetail: true,
      form: row
    })
  },
  // 查看点名详情
  navigateToCallDetail() {
    this.setData({
      showDetail: false
    })
    wx.navigateTo({
      url: `/pages/operation/classRecord/classRecordDetail/index?param=${JSON.stringify(this.data.selectRow)}`,
    })
  },
  onCloseDetail() {
    this.setData({
      showDetail: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})